<%--
 *  File:    AccountingPeriodEdit.jsp
 *  @Author: Langerwang 
 *  Date:    2002/1/23
 *
 *  Copyright (C) 2000 - 2001 Oval Technologies, Inc.
 *  All rights reserved. 
 *
 *  This software is the confidential and proprietary information of Oval 
 *  Technologies, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Oval.
--%>


<%@ include file="/inc/SessionTimeout.inc"%>
<%@ page language="java" %>     
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Category" %>

<%@ page contentType = "text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/PageFlow.tld" prefix="PageFlow" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/oval-tag.tld" prefix="oval" %>

<%@ taglib uri="/WEB-INF/custom-tag.tld" prefix="custom" %>

<%
  //init param
  Category cat = Category.getInstance("jsp.AccountingPeriodEdit");  
  String privilegeCode = "40106";
  String strModuleName = "SPM";
%>



<%
  String strDateFormat = (String)session.getAttribute("dateformat");
  java.sql.Date now = new java.sql.Date((new java.util.Date()).getTime());
%>
<%@ include file="/inc/Security.inc" %>
<%@ include file="/inc/PageDefault.inc" %>

<html>
<head>
<title>AccountingPeriodEdit</title>
<PageFlow:CheckPageFlow pageName="accountingperiodedit"/>
<%@ include file="/inc/InitEdit.inc" %>
<%
  Vector accountingPeriodInfo = (Vector)pageContext.getAttribute("accountingperiodinfo");
  if(accountingPeriodInfo == null)
    accountingPeriodInfo = new Vector();
  
  int specialaccperiodnum=0;
  cat.debug("The accountingPeriodInfo is :" + accountingPeriodInfo);
  if(accountingPeriodInfo.size()>0)
  {
      HashMap firstHM=(HashMap)accountingPeriodInfo.firstElement() ;
      pageContext.setAttribute("firstdate",(firstHM.get("begindate") == null)?"":(OvalTools.dateToString((java.sql.Date)firstHM.get("begindate"),strDateFormat)));
      HashMap lastHM=(HashMap)accountingPeriodInfo.lastElement() ;
      pageContext.setAttribute("lastdate",(lastHM.get("enddate") == null)?"":(OvalTools.dateToString((java.sql.Date)lastHM.get("enddate"),strDateFormat)));
      
      if(firstHM.get("specialaccperiodnum")!=null)
      {
        specialaccperiodnum=((Integer)firstHM.get("specialaccperiodnum")).intValue();
        if(specialaccperiodnum<0) specialaccperiodnum=0;
      } 
   }         
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../css/<%= strLanguageCode %>/main.css" type="text/css">
<script language="JavaScript" src="../../js/FuncLib.js"></script>

<script language="JavaScript">
var flag = true;
function checkbegindate(val,index)
{
    var firstDate=document.accperiodeditform.tempbegindate0.value;
    var lastDateObj=eval("document.accperiodeditform.tempenddate"+(Number(document.accperiodeditform.count.value)-1));
    var lastDate=lastDateObj.value;
 
    with(document.accperiodeditform)
    {
        if(isEmpty(val) || val==" ")
            return "";
        if(!isEmpty(val) && dateCheck(val,"yyyy-MM-dd","<%=JspHelper.filterNull( session.getAttribute("dateformat") )%>") < 0)
         {
           alert("<%= message.getMessageDesc("VALUE_MUST_BE_DATE",label.getLabelName("L_BeginningDate",false)) %>");
           return "";
         }    
        if(toDate(val,'<%=JspHelper.filterNull( strDateFormat )%>')<=toDate(firstDate,'<%=JspHelper.filterNull( strDateFormat )%>') || toDate(val,'<%=JspHelper.filterNull( strDateFormat )%>')>=toDate(lastDate,'<%=JspHelper.filterNull( strDateFormat )%>'))
        {
            alert("<%= message.getMessageDesc("VALUE_MUST_BETWEEN",label.getLabelName("L_BeginningDate",false),(String)pageContext.getAttribute("firstdate"),(String)pageContext.getAttribute("lastdate"))%>");
            return "";
        }
        return val;
    }
}
function dateFormat(val,index,no)
{
  var firstDate=document.accperiodeditform.tempbegindate0.value;
  var lastDateObj=eval("document.accperiodeditform.tempenddate"+(Number(document.accperiodeditform.count.value)-1));
  var lastDate=lastDateObj.value;
  with(accperiodeditform)
  {
    enddatex = eval("tempenddate" + index);
    begindatex = eval("tempbegindate" + index);
    begindatey = eval("tempbegindate" + (new Number(1) + new Number(index)));
    enddatey = eval("tempenddate" + (new Number(1) + new Number(index)));  
    if(isEmpty(val) )
        return "";
         if(!isEmpty(val) && dateCheck(val,"yyyy-MM-dd","<%=JspHelper.filterNull( session.getAttribute("dateformat") )%>") < 0)
         {
           alert("<%= message.getMessageDesc("VALUE_MUST_BE_DATE",label.getLabelName("L_EndingDate",false)) %>");
           return "";
         }
    if(toDate(val,'<%=JspHelper.filterNull( strDateFormat )%>')<=toDate(firstDate,'<%=JspHelper.filterNull( strDateFormat )%>') || toDate(val,'<%=JspHelper.filterNull( strDateFormat )%>')>=toDate(lastDate,'<%=JspHelper.filterNull( strDateFormat )%>'))
    {
        alert("<%= message.getMessageDesc("VALUE_MUST_BETWEEN",label.getLabelName("L_EndingDate",false),(String)pageContext.getAttribute("firstdate"),(String)pageContext.getAttribute("lastdate"))%>");
        return "";
    }
    if(toDate(val,'<%=JspHelper.filterNull( strDateFormat )%>') <= toDate(begindatex.value,'<%=JspHelper.filterNull( strDateFormat )%>'))
    {
      alert("<%= message.getMessageDesc("VALUE_MUST_MORETHAN",label.getLabelName("L_EndingDate",false),label.getLabelName("L_BeginningDate",false)) %>"); 
      return "";
    }
    var tempdate = toDate(enddatex.value,'<%=JspHelper.filterNull( strDateFormat )%>');
    var newVal = new Date(tempdate.getYear(),tempdate.getMonth(),(tempdate.getDate() + 1));
    var nextVal = dateCheck((newVal.getYear() + "-" + (newVal.getMonth() + 1) + "-" + newVal.getDate()),"<%=JspHelper.filterNull( strDateFormat )%>","yyyy-MM-dd");
    if(typeof(nextVal)!="undefined")
        begindatey.value = nextVal;
    return val;
  }
}

function page_done()
{  
    
  for(var i = 0;i < document.accperiodeditform.count.value;i++)
  {

    with(accperiodeditform)
    {
      var tempBegin = eval("tempbegindate" + i);
      var tempEnd = eval("tempenddate" + i);
      var tempStatus = eval("status" + i);
      
      var checktempBegin;
      if(i<11)
      {
          checktempBegin = eval("tempbegindate" + (i+1));
             
		      if(checktempBegin.value <= tempEnd.value)
		      {
		      	alert("<%= message.getMessageDesc("STARTDATE_CAN_NOT_LATE_THAN_ENDDATE")%>");
		      	return;
		      }
      }
      
      if(! isNullOrEmpty(tempBegin.value) &&  !isNullOrEmpty(tempEnd.value) && tempBegin.value!=" " && tempEnd.value!=" ")
      {
          if(i<Number(accperiodeditform.count.value)-<%=JspHelper.filterNull(specialaccperiodnum)%>&&toDate(tempBegin.value,'<%=JspHelper.filterNull( strDateFormat )%>') >= toDate(tempEnd.value,'<%=JspHelper.filterNull( strDateFormat )%>'))
          {
            alert("<%= message.getMessageDesc("VALUE_MUST_MORETHAN",label.getLabelName("L_EndingDate",false),label.getLabelName("L_BeginningDate",false)) %>"); 
            return;
          } 
      }
      
<%--[SCRIPT]  WY: Bug5763
         <#%
         String strAccyearstatus=(request.getParameter("accyearstatus")==null)?"-1":(request.getParameter("accyearstatus"));
         int nAccyearstatus=(new Integer(strAccyearstatus)).intValue();
            
         if(nAccyearstatus==ConstantProp.USED_STATUS){
       %#>
         if(isNullOrEmpty(tempBegin.value) || isNullOrEmpty(tempEnd.value) || toDate(tempBegin.value,'<#%= strDateFormat %#>')==" " || toDate(tempBegin.value,'<#%= strDateFormat %#>')==-1 || toDate(tempEnd.value,'<#%= strDateFormat %#>')==" " || toDate(tempEnd.value,'<#%= strDateFormat %#>')==-1)
         {
            tempStatus.value =27;
         }
         else
         {
              if(toDate("<#%= now %#>","yyyy-MM-dd") < toDate(tempBegin.value,'<#%= strDateFormat %#>')) 
              {
                tempStatus.value =27;
              }
              else if(toDate("<#%= now %#>","yyyy-MM-dd") > toDate(tempEnd.value,'<#%= strDateFormat %#>') ) 
              {
                tempStatus.value = <#%= ConstantProp.CLOSE_STATUS %#>;
              }
              else
              {
                tempStatus.value = <#%= ConstantProp.OPEN_STATUS %#>;
              }
         }
         <#%}%#>
  [SCRIPT]--%>      
    } 

    with(accperiodeditform)
    {
      var tempBegin = eval("tempbegindate" + i);
      var tempEnd = eval("tempenddate" + i);
      var tBegin = eval("begindate" + i);
      var tEnd = eval("enddate" + i);
      var tBeginTemp=dateCheck(tempBegin.value,'yyyy-MM-dd','<%=JspHelper.filterNull( strDateFormat )%>');
      tBegin.value =  (tBeginTemp=="-1")?"":tBeginTemp;
      var tEndTemp= dateCheck(tempEnd.value,'yyyy-MM-dd','<%=JspHelper.filterNull( strDateFormat )%>');
      tEnd.value =   (tEndTemp=="-1")?"":tEndTemp;         
    }           
  }
    
  emptyToNull(document.accperiodeditform);
  
  document.accperiodeditform.actionname.value = "done";
  isRunning = true;
  document.accperiodeditform.submit();
}
</script>
</head>
<body>
<table width="100%" cellspacing="0" border="0" cellpadding="0">
  <form name="accperiodeditform" method="post" action="../../Dispatcher" target="_parent">
      <input type="hidden" name="pagename" value="accountingperiodedit">
      <input type="hidden" name="actionname">   

      <input type="hidden" name="count" value=<%=JspHelper.filterNull( accountingPeriodInfo.size() )%>>   
      <input type="hidden" name="accyearid" value=<%=JspHelper.filterNull( request.getParameter("accyearid") )%>>   
			<input type="hidden" name="accyearstatus">   
			<input type="hidden" name="accperiodid">
			<input type="hidden" name="booksettype" value=1>

  <tr> 
    <td> 
      <table width="100%" cellspacing="0" cellpadding="0">
        <tr> 
          <td colspan="2" class="summary_top"></td>
        </tr>
        <tr> 
          <td colspan="2"><img src="../../images/<%= strLanguageCode %>/quote.gif" width="16" height="11"> 
            <%= label.getLabelName("L_SetAP") + label.getLabelName("L_Colon") %></td>
        </tr>
      </table>
      <table width="100%" border="1" cellpadding="0" bordercolorlight="#999999" bordercolordark="#FFFFFF" cellspacing="0" align="center">       
        <tr> 
          <td class="deepblue-left" width="45">&nbsp;<%= label.getLabelName("L_Sequence") %></td>
          <td class="deepblue-left">&nbsp;<%= label.getLabelName("L_BeginningDate") %></td>
          <td class="deepblue-left"> &nbsp;<%= label.getLabelName("L_EndingDate") %></td>
          <% String strAccyearstatus=request.getParameter("accyearstatus");
         		 if (strAccyearstatus.equals("27")){%>
          <td class="deepblue-left" width="45">&nbsp;<%= label.getLabelName("L_Status") %></td> 
          <%}%>
        </tr>
        

  <%
    int i=0;
    String strClassType = "";  
    String strStyle = "";
    String strRead = "";    
  %>
    <logic:iterate id="acc" collection="<%= accountingPeriodInfo %>"> 
  <%
      HashMap temp = (HashMap)pageContext.getAttribute("acc");
      if(i%2 == 1)
      {
        strClassType = "td-listrom";
      }else{                    
        strClassType = "none";
      } 
    if(((Integer)temp.get("status")).intValue() != 27)
    {
      strStyle = "border:0";
      strRead = "readonly";
    }
    else
    {
      strStyle = "";
      strRead = "";
    } 
  %> 
       
        <tr class="<%=JspHelper.filterNull( strClassType )%>"> 
          <td><%=JspHelper.filterNull( temp.get("no") )%>
           <input type="hidden" name="accperiodid<%=JspHelper.filterNull( i )%>" value="<%=JspHelper.filterNull( temp.get("accperiodid") )%>">
            <input type="hidden" name="status<%=JspHelper.filterNull( i )%>" value="<%=JspHelper.filterNull( temp.get("status") )%>">
            <input type="hidden" name="begindate<%=JspHelper.filterNull( i )%>"  command="emptytonull" >
            <input type="hidden" name="enddate<%=JspHelper.filterNull( i )%>"  command="emptytonull" >
            <input type="hidden" name="no<%=JspHelper.filterNull( i )%>" value="<%=JspHelper.filterNull( temp.get("no") )%>">
            <input type="hidden" name="accyearid<%=JspHelper.filterNull( i )%>" value=<%=JspHelper.filterNull( request.getParameter("accyearid") )%>>
        
          </td>
          <td> 
            <%if(i == 0||i >= accountingPeriodInfo.size()-specialaccperiodnum){%>
            &nbsp;<%= (temp.get("begindate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("begindate"),strDateFormat)) %>
            <input type="hidden" name="tempbegindate<%=JspHelper.filterNull( i )%>" command="emptytonull"  value="<%= (temp.get("begindate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("begindate"),strDateFormat)) %>">            
            <%}else{%>
            &nbsp;<input type="text" class="<%=JspHelper.filterNull( strClassType )%>" name="tempbegindate<%=JspHelper.filterNull( i )%>" value="<%= (temp.get("begindate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("begindate"),strDateFormat)) %>" size="20" style="<%=JspHelper.filterNull( strStyle )%>" <%=JspHelper.filterNull( strRead )%> onblur="javascript:tempbegindate<%=JspHelper.filterNull( i )%>.value=checkbegindate(tempbegindate<%=JspHelper.filterNull( i )%>.value,<%=JspHelper.filterNull( i )%>);">
            <%--span class="red"> *</span></td--%>
            <%}%>
          <td> 
            <%if(i >= accountingPeriodInfo.size()-1-specialaccperiodnum){
            
            %>
            &nbsp;<%= (temp.get("enddate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("enddate"),strDateFormat)) %>
            <input type="hidden" name="tempenddate<%=JspHelper.filterNull( i )%>" command="emptytonull" value="<%= (temp.get("enddate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("enddate"),strDateFormat)) %>">            
            <%}else{%>
            &nbsp;<input type="text" class="<%=JspHelper.filterNull( strClassType )%>" name="tempenddate<%=JspHelper.filterNull( i )%>" size="20" value="<%= (temp.get("enddate") == null)?"":(OvalTools.dateToString((java.sql.Date)temp.get("enddate"),strDateFormat)) %>" style="<%=JspHelper.filterNull( strStyle )%>" <%=JspHelper.filterNull( strRead )%> onblur="javascript:tempenddate<%=JspHelper.filterNull( i )%>.value = dateFormat(tempenddate<%=JspHelper.filterNull( i )%>.value,<%=JspHelper.filterNull( i )%>,'<%=JspHelper.filterNull( temp.get("no") )%>');">
            <%--span class="red">*</span></td--%>
            <%}%>  
            <%
         		 if (strAccyearstatus.equals("27")){%>
            <td>
              <input type="radio" id="acc<%=JspHelper.filterNull( i )%>" name="checkhehe" value="<%=JspHelper.filterNull( temp.get("accperiodid") )%>" class="normalradio"  >
            </td>
            <%}%>
        </tr>  
        <%
          i++;
        %>                      
    </logic:iterate>
       <input type="hidden" name="numrows" value="<%=JspHelper.filterNull( i )%>"> 

        
      </table>
    </td>
  </tr>
  <tr> 
    <td class="summary_top"></td>
  </tr>
  <tr> 
     <td class="td_center">
    <a onclick="page_done()"><custom:button type="1" label="L_Finish" imgsrc="finish.gif" border="0" style="width:60"/></a>
		 <%
         		 if (strAccyearstatus.equals("27")){%>
		<a onclick="page_start()"><custom:button type="1" label="L_Startup" imgsrc="Startup.gif" border="0" style="width:60"/></a>
   <%}%>  
		</td>
  </tr>
</form>
</table>
<script>
<%--[SCRIPT]  document.accperiodeditform.tempenddate0.focus();  [SCRIPT]--%>

function page_start()
{
	var flag = false;
	var radios=document.getElementsByName("checkhehe");
	
	for(var i=0;i<radios.length;i++)
	{
		if(radios[i].checked==true)
	{
		accperiodeditform.accperiodid.value=radios[i].value
			
		flag = true;
		break;
}
}
if(!flag) 
{
alert('<%=message.getMessageDesc("J_ACCPERIOD_NOT_SELECT")%>')
return false
}

  if(!confirm("<%= message.getMessageDesc("ARE_YOU_SURE_ACTIVE",label.getLabelName("L_AccountingYear",false)) %>"))
  {
    return;
  }
  document.accperiodeditform.actionname.value = "active";
  document.accperiodeditform.target = "_parent";  
  isRunning = true;
  document.accperiodeditform.submit();
}
</script>
</body>
</html>


